package com.example.demo.config.activiti;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.net.MalformedURLException;

@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Slf4j
public class ActivitiConfig {

    @Bean
    @ConditionalOnMissingBean
    public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean("processEngineConfiguration")
    @ConditionalOnMissingBean
    public SpringProcessEngineConfiguration processEngineConfiguration(@Qualifier("dataSource") DataSource dataSource,
                                                                       PlatformTransactionManager transactionManager) throws MalformedURLException {

        log.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>> start activiti configuration");
        SpringProcessEngineConfiguration configuration =
                new SpringProcessEngineConfiguration();
        //配置数据源,和系统使用同一个数据源
        configuration.setDataSource(dataSource);
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        configuration.setJobExecutorActivate(true);
        configuration.setHistoryLevel(HistoryLevel.FULL);
        configuration.setProcessDefinitionCacheLimit(10);
        //统一的事务管理
        configuration.setTransactionManager(transactionManager);
        configuration.setActivityFontName("宋体");
        configuration.setLabelFontName("宋体");

        return configuration;
    }

    @Bean("processEngineBean")
    @ConditionalOnMissingBean
    public ProcessEngineFactoryBean processEngineBean(SpringProcessEngineConfiguration springProcessEngineConfiguration){
        ProcessEngineFactoryBean processEngineFactoryBean =
                new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration);
        return processEngineFactoryBean;
    }

    @Bean("processEngine")
    @ConditionalOnMissingBean
    public ProcessEngine processEngine(SpringProcessEngineConfiguration springProcessEngineConfiguration) throws Exception {
        return processEngineBean(springProcessEngineConfiguration).getObject();
    }

    @Bean("runtimeService")
    @ConditionalOnMissingBean
    public RuntimeService runtimeService(ProcessEngine processEngine){
        return processEngine.getRuntimeService();
    }

    @Bean("repositoryService")
    @ConditionalOnMissingBean
    public RepositoryService repositoryService(ProcessEngine processEngine){
        return processEngine.getRepositoryService();
    }

    @Bean("taskService")
    @ConditionalOnMissingBean
    public TaskService taskService(ProcessEngine processEngine){
        return processEngine.getTaskService();
    }

    @Bean("managementService")
    @ConditionalOnMissingBean
    public ManagementService managementService(ProcessEngine processEngine){
        return processEngine.getManagementService();
    }

    @Bean("identityService")
    @ConditionalOnMissingBean
    public IdentityService identityService(ProcessEngine processEngine){
        return processEngine.getIdentityService();
    }

    @Bean("historyService")
    @ConditionalOnMissingBean
    public HistoryService historyService(ProcessEngine processEngine){
        return processEngine.getHistoryService();
    }

    @Bean("formService")
    @ConditionalOnMissingBean
    public FormService formService(ProcessEngine processEngine){
        return processEngine.getFormService();
    }

}
